﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LSQL;
using DevExpress.Utils;
using DevExpress.Utils.MVVM;
using Shopfloormanagement.Pre_Form;

namespace Shopfloormanagement.Forms
{
    public partial class teamAddForm : DevExpress.XtraEditors.XtraForm
    {
        public static teamAddForm instance = new teamAddForm();
        public static int DepID = -1;
        public teamAddForm()
        {
            instance = this;
            InitializeComponent();
        }
        private void teamAddForm_Load(object sender, EventArgs e)
        {
            GetAllDepartment();
            int maxLv = 0;
            if (DepID == -1)
            {
                maxLv = publicFunc.MaxDepartmentLevel();
            }
            else
            {
                DataTable dt = SQL.ReturnDataTable3(SystemInfo.SQL, "Departments", $"ID = {DepID}");
                maxLv = (int)dt.Rows[0]["departmentLevel"];
            }
            cbLevel.Properties.Items.Clear();
            for (int i = maxLv-1; i > 0; i--)
            {
                cbLevel.Properties.Items.Add(i);
            }

            Image[] imgs = new Image[]
            {
                Properties.Resources.Foundry,
                Properties.Resources.Machining,
                Properties.Resources.FISTPA,
                Properties.Resources.Maintenance,
                Properties.Resources.MMPC,
                Properties.Resources.Logistics,
                Properties.Resources.Quality,
                Properties.Resources.PT
            };
            foreach (Image img in imgs)
            {
                imgDepartment.Images.Add(img);
            }

            imgDepartment.CurrentImageIndex = 0;
            cbParent.SelectedIndex = 0;

            if (DepID == -1)
            {
                cbParent.SelectedIndex = -1;
            }
            else
            {
                publicFunc.SetComboBoxEditSelectedByTag(cbParent, DepID);
            }
        }
        private void GetAllDepartment()
        {

            DataTable dtDep = SQL.ReturnDataTableDist(SystemInfo.SQL, "ID,DepartmentName", "Departments", "Del_tag = 0");
            publicFunc.ComboBoxInit(dtDep, cbParent, "DepartmentName", "ID");

        }

        private int parentDepID = 0;
        private void cbParent_SelectedValueChanged(object sender, EventArgs e)
        {
            var selectedItem = cbParent.SelectedItem as publicFunc.ComboBoxItem;
            if (selectedItem != null)
            {
                var tag = selectedItem.Tag; // 这里可以使用tag
                parentDepID = Convert.ToInt32(tag);
            }

        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            int display = Convert.ToInt32(chkDisplay.Checked);
            if (name != "" && cbLevel.Text != "")
            {

                string col = "DepartmentName,FactoryID,ParentDepartmentID,isDisplay,departmentICON,departmentLevel";
                string value = $"'{name}',{SystemInfo.readFactory},{parentDepID},{display},'{publicFunc.ImageToBase64(imgDepartment.CurrentImage)}',{cbLevel.Text}";
                int res = SQL.InsertReturnID(SystemInfo.SQL, "Departments", col, value);
                if (res > 0)
                {
                    XtraMessageBox.Show(LookAndFeel, $"成功添加部门 {name}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    sysSettings.instance.GetAllDepartment();
                    txtName.Text = "";
                    DepID = -1;
                    this.Close();
                }
            }

        }
    }
}